﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using NetDon.WebControls;
using NetDon.eBoss.Data.CollectionClasses;
using NetDon.eBoss.Data.EntityClasses;
using NetDon.eBoss.Data.HelperClasses;
using NetDon.eBoss.Portal.Common;

namespace NetDon.OA.Portal.HtmlTool
{
    public partial class SizeEdit : NetDonPage
    {
        #region Properties
        protected string BatchId
        {
            get
            {
                return Parameters.GetString("BatchId");
            }
        }

        public string Type
        {
            get { return Parameters.GetString("Type"); }
        }
        #endregion

        protected void Page_Load(object sender, EventArgs e)
        {


            if (!IsPostBack)
            {

                CancelButton = btnReturn;
                BindDropDownLists();
                if (!string.IsNullOrEmpty(BatchId))
                {
                    var collections = new SizesettingCollection();
                    collections.GetMulti(SizesettingFields.Batchid == BatchId);
                    foreach (var collection in collections)
                    {
                        SetStyles(collection);
                    }

                }


            }
        }

        private void SetStyles(SizesettingEntity collection)
        {
            if (collection.Sitecode == (int)eBaySites.US)
            {
                txtSize.Text = collection.Size;
                txtUSSizetitle.Text = collection.Sizetitle;
                txtUSSizeDescription.Text = collection.Sizedescription;
            }
            if (collection.Sitecode == (int)eBaySites.UK)
            {
                txtUKSizetitle.Text = collection.Sizetitle;
                txtUKSizeDescription.Text = collection.Sizedescription;
            }
            if (collection.Sitecode == (int)eBaySites.AU)
            {
                txtAUSizetitle.Text = collection.Sizetitle;
                txtAUSizeDescription.Text = collection.Sizedescription;
            }
            if (collection.Sitecode == (int)eBaySites.DE)
            {
                txtDESizetitle.Text = collection.Sizetitle;
                txtDESizeDescription.Text = collection.Sizedescription;
            }
            if (collection.Sitecode == (int)eBaySites.FR)
            {
                txtFRSizetitle.Text = collection.Sizetitle;
                txtFRSizeDescription.Text = collection.Sizedescription;
            }
            if (collection.Sitecode == (int)eBaySites.IT)
            {
                txtITSizetitle.Text = collection.Sizetitle;
                txtITSizeDescription.Text = collection.Sizedescription;
            }
            if (collection.Sitecode == (int)eBaySites.Site1)
            {
                txtSite1Sizetitle.Text = collection.Sizetitle;
                txtSite1SizeDescription.Text = collection.Sizedescription;
            }
            if (collection.Sitecode == (int)eBaySites.Site2)
            {
                txtSite2Sizetitle.Text = collection.Sizetitle;
                txtSite2SizeDescription.Text = collection.Sizedescription;
            }
            if (collection.Sitecode == (int)eBaySites.Site3)
            {
                txtSite3Sizetitle.Text = collection.Sizetitle;
                txtSite3SizeDescription.Text = collection.Sizedescription;
            }
            if (collection.Sitecode == (int)eBaySites.Site4)
            {
                txtSite4Sizetitle.Text = collection.Sizetitle;
                txtSite4SizeDescription.Text = collection.Sizedescription;
            }
        }

        private void BindDropDownLists()
        {

        }




        private bool save()
        {
            SizesettingEntity entity = null;

            if (string.IsNullOrEmpty(BatchId))
            {
                var batchId = Guid.NewGuid().ToString();
                //add
                SaveStyles(new SizesettingEntity() { Sitecode = (int)eBaySites.US }, batchId);
                SaveStyles(new SizesettingEntity() { Sitecode = (int)eBaySites.AU }, batchId);
                SaveStyles(new SizesettingEntity() { Sitecode = (int)eBaySites.UK }, batchId);
                SaveStyles(new SizesettingEntity() { Sitecode = (int)eBaySites.DE }, batchId);
                SaveStyles(new SizesettingEntity() { Sitecode = (int)eBaySites.FR }, batchId);
                SaveStyles(new SizesettingEntity() { Sitecode = (int)eBaySites.IT }, batchId);
                SaveStyles(new SizesettingEntity() { Sitecode = (int)eBaySites.Site1 }, batchId);
                SaveStyles(new SizesettingEntity() { Sitecode = (int)eBaySites.Site2 }, batchId);
                SaveStyles(new SizesettingEntity() { Sitecode = (int)eBaySites.Site3 }, batchId);
                SaveStyles(new SizesettingEntity() { Sitecode = (int)eBaySites.Site4 }, batchId);
            }
            else
            {
                var collections = new SizesettingCollection();
                collections.GetMulti(SizesettingFields.Batchid == BatchId);
                //modify

                SaveStyles(GetEntity(collections, (int)eBaySites.US), BatchId);
                SaveStyles(GetEntity(collections, (int)eBaySites.AU), BatchId);
                SaveStyles(GetEntity(collections, (int)eBaySites.UK), BatchId);
                SaveStyles(GetEntity(collections, (int)eBaySites.DE), BatchId);
                SaveStyles(GetEntity(collections, (int)eBaySites.FR), BatchId);
                SaveStyles(GetEntity(collections, (int)eBaySites.IT), BatchId);
                SaveStyles(GetEntity(collections, (int)eBaySites.Site1), BatchId);
                SaveStyles(GetEntity(collections, (int)eBaySites.Site2), BatchId);
                SaveStyles(GetEntity(collections, (int)eBaySites.Site3), BatchId);
                SaveStyles(GetEntity(collections, (int)eBaySites.Site4), BatchId);
            }

            return true;
        }

        private SizesettingEntity GetEntity(SizesettingCollection collection, int siteCode)
        {
            var entity = collection.FirstOrDefault(c => c.Sitecode == siteCode);
            if (entity == null)
            {
                entity = new SizesettingEntity() { Sitecode = siteCode };
            }
            return entity;
        }


        private void SaveStyles(SizesettingEntity entity, string batchId)
        {
            entity.Size = txtSize.Text;
            if (entity.Sitecode == (int)eBaySites.US)
            {
             
                entity.Sizetitle = txtUSSizetitle.Text;
                entity.Sizedescription = txtUSSizeDescription.Text;

            }
            if (entity.Sitecode == (int)eBaySites.UK)
            {
                entity.Sizetitle = txtUKSizetitle.Text;
                entity.Sizedescription = txtUKSizeDescription.Text;
            }
            if (entity.Sitecode == (int)eBaySites.AU)
            {
                entity.Sizetitle = txtAUSizetitle.Text;
                entity.Sizedescription = txtAUSizeDescription.Text;
            }
            if (entity.Sitecode == (int)eBaySites.DE)
            {
                entity.Sizetitle = txtDESizetitle.Text;
                entity.Sizedescription = txtDESizeDescription.Text;
            }
            if (entity.Sitecode == (int)eBaySites.FR)
            {
                entity.Sizetitle = txtFRSizetitle.Text;
                entity.Sizedescription = txtFRSizeDescription.Text;
            }
            if (entity.Sitecode == (int)eBaySites.IT)
            {
                entity.Sizetitle = txtITSizetitle.Text;
                entity.Sizedescription = txtITSizeDescription.Text;
            }
            if (entity.Sitecode == (int)eBaySites.Site1)
            {
                entity.Sizetitle = txtSite1Sizetitle.Text;
                entity.Sizedescription = txtSite1SizeDescription.Text;
            }
            if (entity.Sitecode == (int)eBaySites.Site2)
            {
                entity.Sizetitle = txtSite2Sizetitle.Text;
                entity.Sizedescription = txtSite2SizeDescription.Text;
            }
            if (entity.Sitecode == (int)eBaySites.Site3)
            {
                entity.Sizetitle = txtSite3Sizetitle.Text;
                entity.Sizedescription = txtSite3SizeDescription.Text;
            }
            if (entity.Sitecode == (int)eBaySites.Site4)
            {
                entity.Sizetitle = txtSite4Sizetitle.Text;
                entity.Sizedescription = txtSite4SizeDescription.Text;
            }
            entity.Batchid = batchId;
            entity.Save();
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {

            if (save())
            {
                Success("保存成功!");
                CloseWindow(true);
            }



        }



    }

}